package com.xiaohe66.web.domain.sys.sec.service;

import com.xiaohe66.common.util.IdWorker;
import com.xiaohe66.web.domain.account.value.AccountId;
import com.xiaohe66.web.domain.sys.sec.agg.SecEncrypt;
import com.xiaohe66.web.domain.sys.sec.repository.SecEncryptRepository;
import com.xiaohe66.web.domain.sys.sec.value.SecEncryptId;
import com.xiaohe66.web.domain.sys.sec.value.SecEncryptPublicKey;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @author xiaohe
 * @since 2024-01-03 11:30
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SecEncryptService {

    private final SecurityService securityService;
    private final SecEncryptRepository sysEncryptRepository;

    public void savePublicKey(AccountId createId, SecEncryptPublicKey publicKey) {

        SecEncrypt dbSecEncrypt = sysEncryptRepository.getByCreateId(createId);

        SecEncryptId id = dbSecEncrypt == null ? new SecEncryptId(IdWorker.genId()) : dbSecEncrypt.getId();

        SecEncrypt secEncrypt = SecEncrypt.builder()
                .id(id)
                .createId(createId)
                .publicKey(publicKey)
                .build();

        sysEncryptRepository.save(secEncrypt);
    }
}
